/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Envios.Vista;

import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Modelo.EstadoEnvio;
import pe.edu.pucp.Modelo.Itinerario;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Modelo.Vuelo;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.ValHelper;

/**
 *
 * @author pachon
 */
public class AdministrarEnviosGUI extends javax.swing.JPanel {

    private JFrame padre;
    
    public AdministrarEnviosGUI() {
        initComponents();
    }
    public JFrame getPadre() {
        return this.padre;
    }
    public void llenaVuelos(ArrayList<Vuelo> lst) {
        if(lst != null){
            for (Vuelo c : lst){
                cmbVueloEnvio.addItem(c.getIdvuelo());
            }
        }
    }
    
    public void llenaEstados(ArrayList<EstadoEnvio> lst) {
        if(lst != null){
            for (EstadoEnvio c : lst){
                cmbEstadoEnvio.addItem(c.getDescripcion());
            }
        }            
    }
    
    public String getCliente(){
        return txtCliente.getText();
    }
    
    public int getVuelo(){
        return cmbVueloEnvio.getSelectedIndex();
    }
    
    public int getEstado(){
        return cmbEstadoEnvio.getSelectedIndex();
    }
               
    public void mostrarPermisos(ArrayList<Permiso> list){            
        if (!list.isEmpty()){                                
            btnBuscar.setEnabled(buscarPermiso(list,Constante.P_ENV_ALM_BUSCAR)==1);
            btnEntrada.setEnabled(buscarPermiso(list,Constante.P_ENV_ALM_ENTRADA)==1);
            btnSalida.setEnabled(buscarPermiso(list,Constante.P_ENV_ALM_SALIDA)==1);
            btnEntrega.setEnabled(buscarPermiso(list,Constante.P_ENV_ALM_REGISTRAR)==1);            
        }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso opc : list){
            if (opc.getElemento().getIdelemento()==idel){ per =opc.getPermiso(); }
        }
        return per;
    }  
    
    public void addBuscarButtonListener(ActionListener listener){      
       btnBuscar.addActionListener(listener);
    }
    
    public void addEntradaButtonListener(ActionListener listener){      
       btnEntrada.addActionListener(listener);
    }
    
    public void addSalidaButtonListener(ActionListener listener){      
       btnSalida.addActionListener(listener);
    }
    
    public void addLimpiarButtonListener(ActionListener listener){      
       btnLimpiar.addActionListener(listener);
    }
    
    public void addEntregarButtonListener(ActionListener listener){      
       btnEntrega.addActionListener(listener);
    }
    
    public void limpiar(){
        txtCliente.setText("");
        ((DefaultTableModel) this.tblResultadosEntrantes.getModel()).setRowCount(0);        
    }
    
    public int getSelectedVueloEntrante(){
        return tblResultadosEntrantes.getSelectedRow();    
    }
    
    public int getSelectedVueloSaliente(){
        return tblResultadosSalientes.getSelectedRow();    
    }
    
    public void llenaGrillaEntrante(ArrayList<Itinerario> lista){        
        SimpleDateFormat formato = new SimpleDateFormat("hh:mm:ss");
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultadosEntrantes.getModel()).setRowCount(0);
            for (int f = 0; f<lista.size();f++){
                ((DefaultTableModel) tblResultadosEntrantes.getModel()).setRowCount(tblResultadosEntrantes.getRowCount()+1);
               
                tblResultadosEntrantes.setValueAt(lista.get(f).getVuelo().getIdvuelo(),f,0);
                tblResultadosEntrantes.setValueAt(lista.get(f).getVuelo().getAeropuertoByIdorigen().getCiudad().getDescripcion(),f,1);
                tblResultadosEntrantes.setValueAt(lista.get(f).getVuelo().getAeropuertoByIddestino().getCiudad().getDescripcion(),f,2);
                tblResultadosEntrantes.setValueAt(formato.format(lista.get(f).getVuelo().getSalidaest()),f,3);
                tblResultadosEntrantes.setValueAt(lista.get(f).getEnvio().getEstadoEnvio().getDescripcion(), f, 4);
             }
        }
    }
    
    public void llenaGrillaSaliente(ArrayList<Itinerario> lista){
        SimpleDateFormat formato = new SimpleDateFormat("hh:mm:ss");

        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultadosSalientes.getModel()).setRowCount(0);
            for (int f = 0; f<lista.size();f++){
                ((DefaultTableModel) tblResultadosSalientes.getModel()).setRowCount(tblResultadosSalientes.getRowCount()+1);
               
                tblResultadosSalientes.setValueAt(lista.get(f).getVuelo().getIdvuelo(),f,0);
                tblResultadosSalientes.setValueAt(lista.get(f).getVuelo().getAeropuertoByIdorigen().getCiudad().getDescripcion(),f,1);
                tblResultadosSalientes.setValueAt(lista.get(f).getVuelo().getAeropuertoByIddestino().getCiudad().getDescripcion(),f,2);
                tblResultadosSalientes.setValueAt(formato.format(lista.get(f).getVuelo().getSalidaest()),f,3);
                tblResultadosSalientes.setValueAt(lista.get(f).getEnvio().getEstadoEnvio().getDescripcion(), f, 4);
             }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbEstadoEnvio = new javax.swing.JComboBox();
        txtCliente = new javax.swing.JTextField();
        btnLimpiar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        cmbVueloEnvio = new javax.swing.JComboBox();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultadosEntrantes = new javax.swing.JTable();
        btnEntrada = new javax.swing.JButton();
        btnEntrega = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblResultadosSalientes = new javax.swing.JTable();
        btnSalida = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Envios"));

        jLabel1.setText("Vuelo de Envío :");

        jLabel2.setText("Estado:");

        jLabel3.setText("Cliente relacionado:");

        cmbEstadoEnvio.setPreferredSize(new java.awt.Dimension(108, 27));

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N
        btnLimpiar.setToolTipText("Limpiar");

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N
        btnBuscar.setToolTipText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(cmbEstadoEnvio, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmbVueloEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbVueloEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEstadoEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tblResultadosEntrantes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "VUELO ENVÍO", "CIUDAD ORIGEN", "CIUDAD DESTINO", "HORA DE SALIDA", "ESTADO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultadosEntrantes.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultadosEntrantes);

        btnEntrada.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/plus.png"))); // NOI18N
        btnEntrada.setToolTipText("Nota Entrada");

        btnEntrega.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/checkmark.png"))); // NOI18N
        btnEntrega.setToolTipText("Finalizar Envio");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Envios Entrantes", jPanel3);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tblResultadosSalientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "VUELO ENVÍO", "CIUDAD ORIGEN", "CIUDAD DESTINO", "HORA DE SALIDA", "ESTADO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultadosSalientes.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblResultadosSalientes);

        btnSalida.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/minus.png"))); // NOI18N
        btnSalida.setToolTipText("Nota Salida");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalida)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalida)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Envios Salientes", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTabbedPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("Busqueda Envíos");
        jPanel1.getAccessibleContext().setAccessibleDescription("");
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEntrada;
    private javax.swing.JButton btnEntrega;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnSalida;
    private javax.swing.JComboBox cmbEstadoEnvio;
    private javax.swing.JComboBox cmbVueloEnvio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tblResultadosEntrantes;
    private javax.swing.JTable tblResultadosSalientes;
    private javax.swing.JTextField txtCliente;
    // End of variables declaration//GEN-END:variables
}

